package org.mysfit.ctp4j.core.service.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.mysfit.ctp4j.core.service.CommonService;
import org.mysfit.ctp4j.infra.pojo.CtpConfig;
import org.mysfit.ctp4j.infra.pojo.CtpUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * 
 * @author long.gf
 * @since 2022-12-06
 *
 */
@Service
public class CommonServiceImpl implements CommonService {
	
	@Autowired
	private CtpConfig ctpConfig;
	
	private CtpUser currUser;
	
	@Override
	public CtpConfig getConfig() {
		return ctpConfig;
	}

	@Override
	public CtpUser getUser(String investorId) {
		List<CtpUser> ctpUsers = ctpConfig.getUsers();
		if(CollectionUtils.isEmpty(ctpUsers)) {
			throw new RuntimeException("缺少配置");
		}
		Map<String, CtpUser> ctpUsersMap = ctpUsers.stream().collect(Collectors.toMap(CtpUser::getInvestorId, o -> o));
		CtpUser currCtpUser = ctpUsersMap.get(investorId);
		if(null == currCtpUser) {
			throw new RuntimeException(String.format("没有找到用户[%s]相关配置", investorId));
		}
		this.setCurrUser(currCtpUser);
 		return currCtpUser;
	}

	public CtpConfig getCtpConfig() {
		return ctpConfig;
	}

	public void setCtpConfig(CtpConfig ctpConfig) {
		this.ctpConfig = ctpConfig;
	}

	public CtpUser getCurrUser() {
		return currUser;
	}

	public void setCurrUser(CtpUser currUser) {
		this.currUser = currUser;
	}


}
